package edu.tgc.entity.system;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;

import edu.tgc.entity.hrm.HrmEmployee;
import edu.tgc.entity.hrm.IdCade;

@Entity
@Table(indexes = { @Index(columnList = "username asc", unique = true), @Index(columnList = "createTime asc"), @Index(columnList = "enabled asc") })
public class SysUser extends HrmEmployee {

    private static final long serialVersionUID = -8178538442592411374L;
    
    protected IdCade cade;
    protected String username;
    @JsonIgnore
    protected String password;
    protected SysDept dept;
    @JsonIgnore
    private Set<SysDeptPosition> deptPositions = new HashSet<>();
    protected String description;
    protected Boolean enabled = true;
    protected String ip;
    @JsonIgnore
    private Set<SysRole> roles = new HashSet<>();
    protected Integer sort = 1;

    public SysUser() {
    }

    public SysUser(UUID id) {
        this.id = id;
    }

    /*public SysUser(String name) {
		super();
		this.cade.setName(name);
	}*/

	@ManyToOne
    @JoinColumn(name = "dept_id")
    public SysDept getDept() {
        return this.dept;
    }

    @ElementCollection
    @CollectionTable(name = "sys_user_position", joinColumns = @JoinColumn(name = "user_id"))
    public Set<SysDeptPosition> getDeptPositions() {
        return this.deptPositions;
    }

    @Column(name = "description", length = 255)
    public String getDescription() {
        return this.description;
    }

    @Column(name = "enabled")
    public Boolean getEnabled() {
        return this.enabled;
    }

    @Column(name = "ip", length = 39)
    public String getIp() {
        return this.ip;
    }


    @Column(name = "password", length = 255)
    public String getPassword() {
        return this.password;
    }

    @OrderBy(value = "sort asc")
    @ManyToMany
    @JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    public Set<SysRole> getRoles() {
        return this.roles;
    }

    @Column(name = "sort")
    public Integer getSort() {
        return this.sort;
    }

    @Column(name = "username", length = 31, unique = true)
    public String getUsername() {
        return this.username;
    }

    public void setDept(SysDept dept) {
        this.dept = dept;
    }

    public void setDeptPositions(Set<SysDeptPosition> deptPositions) {
        this.deptPositions = deptPositions;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }


    public void setPassword(String password) {
        this.password = password;
    }

    public void setRoles(Set<SysRole> roles) {
        this.roles = roles;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public void setUsername(String username) {
        this.username = username;
    }
    @ManyToOne
	public IdCade getCade() {
		return cade;
	}

	public void setCade(IdCade cade) {
		this.cade = cade;
	}
    
	@Transient
	public String getName(){
		return cade==null?"":cade.getName();
	}
    
    
}
